package com.tenton;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

/**
 * @Author:Tenton
 * @Date: 2020/10/31 15:40
 * @Description: 游戏面板
 */
public class GamesPanel extends JPanel implements KeyListener, ActionListener {
    //蛇的长度
    int lenght;
    //蛇的坐标X
    int[] snakeX = new int[600];
    //蛇的坐标Y
    int[] snakeY = new int[500];
    //方向  U：上   D：下  L：左  R：右
    String fx;
    //游戏是否开始
    boolean isStart = false;
    //定时器
    Timer timer = new Timer(1000,this);
    //定义一个食物
    int food1X;int food1Y;
    int food2X;int food2Y;
    int food3X;int food3Y;
    Random random = new Random();
    //死亡判断
    boolean isFail = false;
    //积分系统
    int score;
    //构造器
    public GamesPanel(){
        init();
        //获取键盘的监听事件
        this.setFocusable(true);
        this.addKeyListener(this);
        //让时间动起来
        timer.start();
    }
    //初始化
    public void init(){
        //蛇的长度
        lenght = 3;
        //蛇的头部
        snakeX[0] = 100; snakeY[0] = 100;
        //第一个身体坐标
        snakeX[1] = 75; snakeY[1] = 100;
        //第二个身体坐标
        snakeX[2] = 50; snakeY[2] = 100;
        //蛇头方向
        fx = "R";
        //随机生成食物
        food1X = 25 + 25*random.nextInt(34);
        food1Y = 75 + 25*random.nextInt(24);
        food2X = 25 + 25*random.nextInt(34);
        food2Y = 75 + 25*random.nextInt(24);
        food3X = 25 + 25*random.nextInt(34);
        food3Y = 75 + 25*random.nextInt(24);
        //积分
        score = 0;
    }
    /**
     * 2、画板：画界面，画蛇
     * Graphics ：画笔
     */
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); //清屏
        //设置背景颜色 bai色
        this.setBackground(Color.WHITE);
        //绘制头部的图片
        Data.header.paintIcon(this,g,20,11);
        //绘制游戏的区域
        g.fillRect(25,65,840,600);

        //画一条静态的小蛇
        if (fx.equals("U")){
            Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);
        }else if (fx.equals("D")){
            Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);
        }else if (fx.equals("L")){
            Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);
        }else if (fx.equals("R")){
            Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
        }

        //蛇的身体长度通过length来控制
        for (int i = 1;i < lenght;i++){
            Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
        }
        //画积分
        g.setColor(Color.WHITE);
        g.setFont(new Font("微软雅黑",Font.BOLD,18));
        g.drawString("长度："+lenght,750,35);
        g.drawString("分数："+score,750,50);
        //画食物
        Data.food1.paintIcon(this,g,food1X,food1Y);
        Data.food2.paintIcon(this,g,food2X,food2Y);
        Data.food3.paintIcon(this,g,food3X,food3Y);
        //游戏提示：是否开始
        if (isStart == false){
            //画一个文字，String
            //设置画笔颜色
            g.setColor(Color.WHITE);
            //设置字体
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按下空格开始游戏",300,300);

        }
        //失败提示
        if (isFail){
            //画一个文字，String
            //设置画笔颜色
            g.setColor(Color.RED);
            //设置字体
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("游戏失败，按下空格重新开始游戏",200,300);

        }
    }
    //3、接收键盘的输入：监听

    @Override
    public void keyPressed(KeyEvent e) {
        //键盘按下，未释放
        //获取按下的键盘是哪个键
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_SPACE){//如果按下的是空格键
            //游戏失败，再来一次
            if (isFail){
                isFail = false;
                //重新初始化游戏
                init();
            }else {
                //暂停游戏
                isStart = !isStart;
            }
            //重新绘制界面  类似于 刷新界面
            repaint();
        }
        //键盘控制走向
        if (keyCode == KeyEvent.VK_LEFT){
            fx = "L";
        }else if (keyCode == KeyEvent.VK_UP){
            fx = "U";
        }else if (keyCode == KeyEvent.VK_RIGHT){
            fx = "R";
        }else if (keyCode == KeyEvent.VK_DOWN){
            fx = "D";
        }
    }

    //4、定时器：监听时间 帧
    @Override
    public void actionPerformed(ActionEvent e) {
        //执行定时操作
        //如果游戏处于开始状态，并且游戏没有结束
        if (isStart && isFail ==false){
            //右移
            //身体移动 除了头部，身体都向前移动
            for (int i = lenght -1;i > 0;i--){
                snakeX[i] = snakeX[i-1];
                snakeY[i] = snakeY[i-1];
            }
            //通过控制方向让头部移动
            if (fx.equals("R")){
                //头部移动
                snakeX[0] += 25;
                //边界判断
                if (snakeX[0] > 850){
//                    snakeX[0] = 25;
                    isFail = true;
                }
            }else if (fx.equals("L")){
                //头部移动
                snakeX[0] -= 25;
                //边界判断
                if (snakeX[0] < 25){
//                    snakeX[0] = 850;
                    isFail = true;
                }
            }else if (fx.equals("U")){
                //头部移动
                snakeY[0] -= 25;
                //边界判断
                if (snakeY[0] <75){
//                    snakeY[0] = 650;
                    isFail = true;
                }
            }else if (fx.equals("D")){
                //头部移动
                snakeY[0] += 25;
                //边界判断
                if (snakeY[0] > 650){
//                    snakeY[0] = 75;
                    isFail = true;
                }
            }
            //如果小蛇的头与食物坐标重合
            if (snakeX[0] == food1X && snakeY[0] == food1Y){
                //长度+1
                lenght++;
                score += 1;
                //重新生成食物
                food1X = 25 + 25*random.nextInt(34);
                food1Y = 75 + 25*random.nextInt(24);
            }
            if (snakeX[0] == food2X && snakeY[0] == food2Y){
                //长度+1
                lenght++;
                score += 2;
                //重新生成食物
                food2X = 25 + 25*random.nextInt(34);
                food2Y = 75 + 25*random.nextInt(24);
            }
            if (snakeX[0] == food3X && snakeY[0] == food3Y){
                //长度+1
                lenght++;
                score += 3;
                //重新生成食物
                food3X = 25 + 25*random.nextInt(34);
                food3Y = 75 + 25*random.nextInt(24);
            }
            if (score <= 10){
                timer.setDelay(1000);
            }else if (score > 10 && score <= 100){
                timer.setDelay(500);
            }else if (score > 100 && score <= 10000){
                timer.setDelay(100);
            }else if (score > 10000){
                timer.setDelay(10);
            }
            //结束判断
            for (int i = 1;i < lenght;i++){
                if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]){
                    isFail = true;
                }
            }
            //刷新界面
            repaint();
        }
        //让时间动起来
        timer.start();
    }
    @Override
    public void keyTyped(KeyEvent e) {
        //键盘按下，弹起 : 敲击
    }

    @Override
    public void keyReleased(KeyEvent e) {
        //释放某个键
    }

}